package com.selfdiscipline.ui.screens.task

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.selfdiscipline.R
import com.selfdiscipline.ui.components.TaskCard
import com.selfdiscipline.ui.screens.task.components.TaskList

/**
 * Main task screen showing task list and add task button
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TaskScreen(
    viewModel: TaskViewModel = hiltViewModel(),
    navigateToTaskDetail: (Long) -> Unit,
    navigateToAddTask: () -> Unit = {}
) {
    val uiState by viewModel.uiState.collectAsState()
    
    Scaffold(
        floatingActionButton = {
            FloatingActionButton(
                onClick = { navigateToAddTask() }
            ) {
                Icon(
                    imageVector = Icons.Filled.Add,
                    contentDescription = stringResource(R.string.task_add)
                )
            }
        },
        topBar = {
            TopAppBar(
                title = { 
                    Text(text = stringResource(id = R.string.nav_task)) 
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primary,
                    titleContentColor = MaterialTheme.colorScheme.onPrimary
                )
            )
        }
    ) { paddingValues ->
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues),
            contentAlignment = Alignment.Center
        ) {
            if (uiState.isLoading) {
                CircularProgressIndicator()
            } else if (uiState.tasks.isEmpty()) {
                Text(
                    text = "没有待办任务",
                    style = MaterialTheme.typography.bodyLarge,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.padding(16.dp)
                )
            } else {
                TaskList(
                    tasks = uiState.tasks,
                    onTaskClick = navigateToTaskDetail,
                    onTaskComplete = { task -> viewModel.completeTask(task) },
                    contentPadding = PaddingValues(16.dp)
                )
            }
        }
    }
}

@Composable
private fun TaskList(
    tasks: List<com.selfdiscipline.data.local.entity.TaskEntity>,
    onTaskClick: (Long) -> Unit,
    onTaskComplete: (com.selfdiscipline.data.local.entity.TaskEntity) -> Unit,
    contentPadding: PaddingValues
) {
    LazyColumn(
        contentPadding = contentPadding
    ) {
        items(tasks) { task ->
            TaskCard(
                task = task,
                onTaskComplete = onTaskComplete,
                onTaskClick = { onTaskClick(it.id) }
            )
        }
    }
} 